<?php

namespace app\admin\controller;
use app\admin\logic\GoodsLogic;
use think\AjaxPage;
use think\Page;
use think\Db;


class Cellar extends Base {
	
	private  $limit = 10;
    
	/*
     * 初始化操作
     */
    public function _initialize() {
        parent::_initialize();
        $this->order_status = C('CELLAR_ORDER_STATUS');
        $this->pay_status = C('PAY_STATUS');
        // 订单 支付 发货状态
        $this->assign('order_status',$this->order_status);
        $this->assign('pay_status',$this->pay_status);
    }
	
    
    /**
     *  商品列表
     */
    public function goodsList(){                
     
        $GoodsLogic = new GoodsLogic();        
        $brandList = $GoodsLogic->getSortBrands();
        $categoryList = $GoodsLogic->getSortCategory();
        $this->assign('categoryList',$categoryList);
        $this->assign('brandList',$brandList);
        return $this->fetch();

    }
    
    /**
     *  ajax请求商品列表
     */
    public function ajaxGoodsList(){            
        
        $where = ' 1 = 1 '; // 搜索条件                
        I('intro')    && $where = "$where and ".I('intro')." = 1" ;        
        I('brand_id') && $where = "$where and brand_id = ".I('brand_id') ;
        (I('is_on_sale') !== '') && $where = "$where and is_on_sale = ".I('is_on_sale') ;                
        $cat_id = I('cat_id');
        // 关键词搜索               
        $key_word = I('key_word') ? trim(I('key_word')) : '';
        if($key_word)
        {
            $where = "$where and (goods_name like '%$key_word%' or goods_sn like '%$key_word%')" ;
        }
        
        if($cat_id > 0)
        {
            $grandson_ids = getCatGrandson($cat_id); 
            $where .= " and cat_id in(".  implode(',', $grandson_ids).") "; // 初始化搜索条件
        }
        
        
        $model = M('Goods');
        $where = "$where and type = 1";//只查询窖藏产品
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        /**  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
        */
        $show = $Page->show();
        $order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
        $goodsList = $model->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();

        $catList = D('goods_category')->select();
        $catList = convert_arr_key($catList, 'id');
        $this->assign('catList',$catList);
        $this->assign('goodsList',$goodsList);
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
    }
    
	/**
     *  添加窖藏商品
     */
    public function addeditGoods(){
    	$GoodsLogic = new GoodsLogic();
        $Goods = D('Goods');
		if(I('id')&&is_numeric(I('id'))){
				$goods_id = I('id');
				$g1 = M('goods')->where(array('goods_id'=>$goods_id))->find();
				$g2 = M('cellar_goods_attr')->where(array('goods_id'=>$goods_id))->find();
				if($g2){
					$g2['theme_id'] = json_decode($g2['theme_id'],true);
					$g2['type_id'] = json_decode($g2['type_id'],true);
					$g2['aroma_id'] = json_decode($g2['aroma_id'],true);
					$g2['honor_images'] = json_decode($g2['honor_images'],true);//转换荣誉图片
					$g2['quantity'] = json_decode($g2['quantity'],true);//转换荣誉图片
				}
				$goodsInfo = array_merge($g1,$g2);
				$this->assign('goodsInfo',$goodsInfo);
		}
    	if(IS_POST){
 
		    $data = I('post.'); 
			//拆分数据
			array_pop($data['honor_images']);
			$data1 = array(//cellar_goods_attr附属表数据
			       'theme_id' => empty($data['theme_id'])?'':json_encode($data['theme_id']),
			       'type_id' => empty($data['type_id'])?'':json_encode($data['type_id']),
//			       'cat_id' => $data['cat_id'],
			       'aroma_id' => empty($data['aroma_id'])?'':json_encode($data['aroma_id']),
			       'winery_id' => $data['winery_id'],
			       'degree' => $data['degree'],
			       'quantity' => empty($data['quantity'])?'':json_encode($data['quantity']),
			       'material' => $data['material'],
			       'productiontime' => strtotime($data['productiontime']),
			       'bar_code' => $data['bar_code'],
			       'licence_code' => $data['licence_code'],
			       'grade' => $data['grade'],
			       'd_start' => $data['d_start'],
			       'd_end' => $data['d_end'],
			       'honor_images' => empty($data['honor_images'])?'':json_encode($data['honor_images']),   
			); 
			$data2 = array(//goods主表数据
			       'goods_name' => $data['goods_name'],
			       'goods_sn' => $data['goods_sn'],
			       'shop_price' => $data['shop_price'],
			       'market_price' => $data['market_price'],
			       'give_integral' => $data['give_integral'],
			       'store_count' => $data['store_count'],
			       'original_img' => $data['original_img'],
			       'keywords' => $data['keywords'],
			       'weight' =>$data['weight'],
			       'suppliers_id' => $data['winery_id'],
			       'goods_content' => $data['goods_content'],
			       'deduction_amount' => $data['deduction_amount'],
			       'is_distribut' => $data['is_distribut'],
			       'last_update' => time(),
			       'type' => 1,
			       'shipping_area_ids' => implode(',',I('shipping_area_ids/a',[])),
			       
			); 
			if($goods_id){
				M('goods')->where(array('goods_id'=>$goods_id))->save($data2);
				M('cellar_goods_attr')->where(array('goods_id'=>$goods_id))->save($data1);
				// 修改商品后购物车的商品价格也修改一下
                M('cart')->where("goods_id = $goods_id and spec_key = ''")->save(array(
                        'market_price'=>I('market_price'), //市场价
                        'goods_price'=>I('shop_price'), // 本店价
                        'member_goods_price'=>I('shop_price'), // 会员折扣价                        
                        ));            
			}else{
				$data2['on_time'] = time();//上架时间
				M('goods')->add($data2); // 写入数据到数据库                   
                $data1['goods_id'] = $goods_id = M('goods')->getLastInsID();
				M('cellar_goods_attr')->add($data1);   
			}  
			$Goods->afterSave($goods_id);//处理商品图片
            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('admin/Cellar/goodsList')),
            );
	    	return  $this->ajaxReturn($return_arr); 
    	}

        $theme = M('cellar_theme')->field('id,title')->select();//窖藏主题
		$cellarType = M('cellar_type')->field('id,title')->select();//窖藏方式
		$aroma = M('cellar_aroma')->field('id,title')->select();//香型
		$quantity = M('cellar_quantity')->field('id,val')->order('val')->select();//净含量规格
		$suppliers = M('suppliers')->field('suppliers_id,suppliers_name')->select();//原产地酒厂
        $this->assign('theme',$theme);
		$this->assign('cellarType',$cellarType);
		$this->assign('aroma',$aroma);
		$this->assign('quantity',$quantity);
		$this->assign('suppliers',$suppliers);
		$plugin_shipping = M('plugin')->where(array('type'=>array('eq','shipping')))->select();//插件物流
        $shipping_area = D('Shipping_area')->getShippingArea();//配送区域
        $goods_shipping_area_ids = explode(',',$goodsInfo['shipping_area_ids']);
        $goodsImages = M("GoodsImages")->where(array('goods_id'=>$goods_id))->select();
        $this->assign('goodsImages', $goodsImages);  // 商品相册
		$this->assign('goods_shipping_area_ids',$goods_shipping_area_ids);
        $this->assign('shipping_area',$shipping_area);
        $this->assign('plugin_shipping',$plugin_shipping);
		
        return $this->fetch();            

    }
	
	
	/**
     *  删除窖藏商品
     */
    /**
     * 删除商品
     */
    public function delGoods()
    {
        $goods_id = $_GET['id'];
        $error = '';
        
        // 判断此商品是否有订单
        $c1 = M('OrderGoods')->where("goods_id = $goods_id")->count('1');
        $c1 && $error .= '此商品有订单,不得删除! <br/>';
         
        
         // 商品退货记录
        $c1 = M('return_goods')->where("goods_id = $goods_id")->count('1');
        $c1 && $error .= '此商品有退货记录,不得删除! <br/>';
        
        if($error)
        {
            $return_arr = array('status' => -1,'msg' =>$error,'data'  =>'',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);        
            $this->ajaxReturn($return_arr);
        }
        
        // 删除此商品        
        M("Goods")->where('goods_id ='.$goods_id)->delete();  //商品表
        M("cart")->where('goods_id ='.$goods_id)->delete();  // 购物车
        M("comment")->where('goods_id ='.$goods_id)->delete();  //商品评论
        M("goods_consult")->where('goods_id ='.$goods_id)->delete();  //商品咨询
        M("goods_images")->where('goods_id ='.$goods_id)->delete();  //商品相册
        M("spec_goods_price")->where('goods_id ='.$goods_id)->delete();  //商品规格
        M("spec_image")->where('goods_id ='.$goods_id)->delete();  //商品规格图片
        M("goods_attr")->where('goods_id ='.$goods_id)->delete();  //商品属性     
        M("goods_collect")->where('goods_id ='.$goods_id)->delete();  //商品收藏    
        M("cellar_goods_attr")->where('goods_id ='.$goods_id)->delete();  //窖藏商品属性       
                     
        $return_arr = array('status' => 1,'msg' => '操作成功','data'  =>'',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);        
        $this->ajaxReturn($return_arr);
    }
	
	
    /**
     *  窖藏主题
     */
    public function cellarTheme(){                
        $model = M('cellar_theme');
        $page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $themesList = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('themeList',$themesList);
        return $this->fetch();
    }
    
    /**
     *  添加更新窖藏主题
     */
    public function addeditTheme(){ 
    	    	
    	$model = M('cellar_theme');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('theme',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		
    		if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '主题名不能为空',
    		        'data' => '主题名不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		if($id){
    			$data['edittime'] = time();
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $data['edittime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/cellarTheme')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    /**
     *  删除窖藏主题
     */
    public function delTheme(){ 
    	    	
    	$model = M('cellar_theme');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/cellarTheme')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/cellarTheme')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    }
    
    
    /**
     *  白酒香型
     */
    public function wineAroma(){ 
    	$model = M('cellar_aroma');
    	$page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $aromaList = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('aromaList',$aromaList);
        return $this->fetch();               
    }
    
    /**
     *  添加更新白酒香型
     */
    public function addeditAroma(){ 
    	    	
    	$model = M('cellar_aroma');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('aroma',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '香型名称不能为空',
    		        'data' => '香型名称不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		
    		if($id){
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/wineAroma')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    

    /**
     *  删除白酒香型
     */
    public function delAroma(){ 
    	    	
    	$model = M('cellar_aroma');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/wineAroma')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/wineAroma')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    }        
    
    
    
    
    /**
     *  窖藏方式
     */
    public function cellarType(){ 
    	$model = M('cellar_type');
    	$page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $cellartype = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('cellartype',$cellartype);                       
        return $this->fetch();
    }
    
    
    /**
     *  添加更新窖藏方式
     */
    public function addeditType(){ 
    	    	
    	$model = M('cellar_type');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('cellartype',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '窖藏方式不能为空',
    		        'data' => '窖藏方式不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		if($id){
    			$data['edittime'] = time();
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $data['edittime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/cellarType')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    
    /**
     *  删除窖藏方式
     */
    public function delType(){ 
    	    	
    	$model = M('cellar_type');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/cellarType')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/cellarType')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    } 
    
    
    /**
     *  最新活动
     */
    public function newActivity(){
    	$model = M('cellar_activity');
    	$page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('activitys',$res);                    
        return $this->fetch();
    }
    
    
    /**
     *  添加更新最新活动
     */
    public function addeditActivity(){ 
    	    	
    	$model = M('cellar_activity');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('activity',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '活动标题不能为空',
    		        'data' => '活动标题不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}elseif (empty(I('post.acurl'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '活动链接不能为空',
    		        'data' => '活动链接不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		$data['publishtime'] = strtotime($data['publishtime']);
    		$data['starttime'] = strtotime($data['starttime']);
    		$data['endtime'] = strtotime($data['endtime']);
    		if($id){
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/newActivity')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    
    /**
     *  删除最新活动
     */
    public function delActivity(){ 
    	    	
    	$model = M('cellar_activity');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/newActivity')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/newActivity')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    } 
    
    
    /**
     *  白酒文化
     */
    public function wineCulture(){ 
    	$model = M('cellar_culture');
    	$page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('cultures',$res);
        return $this->fetch();
    }
    
    
    /**
     *  添加更新白酒文化
     */
    public function addeditCulture(){ 
    	    	
    	$model = M('cellar_culture');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('culture',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '视频名称不能为空',
    		        'data' => '视频名称不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}elseif (empty(I('post.videourl'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '视频链接不能为空',
    		        'data' => '视频链接不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		if($id){
    			$data['edittime'] = time();
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    			$data['edittime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/wineCulture')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    
    /**
     *  删除白酒文化
     */
    public function delCulture(){ 
    	    	
    	$model = M('cellar_culture');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/wineCulture')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/wineCulture')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    } 
    
    
    /**
     *  酒厂体验
     */
    public function wineryTaste(){                
        $model = M('cellar_taste');
        $page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('tastes',$res);
        return $this->fetch();
    }
    
    
    /**
     *  添加更新酒厂体验
     */
    public function addeditTaste(){ 
    	    	
    	$model = M('cellar_taste');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('taste',$res);
        }
    	if(IS_POST){
    	   $data = I('post.');
    	   if(empty(I('post.name'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '酒厂名称不能为空',
    		        'data' => '酒厂名称不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		$data['signtime'] = strtotime($data['signtime']);
    		$data['tastetime'] = strtotime($data['tastetime']);
    		if($id){
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/wineryTaste')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    
    /**
     *  删除酒厂体验
     */
    public function delTaste(){ 
    	    	
    	$model = M('cellar_taste');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		//关联删除报名表中的相关数据
        		M('taste_form')->where(array('tid'=>$id))->delete();
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/wineryTaste')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/wineryTaste')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    }
    
     /**
     *  酒厂体验报名人员管理
     */
    public function tasterManage(){ 
    	
    	//必须传入活动ID
        if(I('tid')&&is_numeric(I('tid')) || I('post.tid')){
        	$tid = I('tid');
        	$taste = M('cellar_taste');
        	$tinfo = $taste->where(array('id'=>$tid))->find();
        	$this->assign('tinfo',$tinfo);
        }
        else{
        	$return_arr = array(
                'status' => -1,
                'msg' => '操作失败',
                'data' => '未传入活动ID',
            );
            return  $this->ajaxReturn($return_arr);
        }               
        $model = M('cellar_taste_form');
        $where = array('tid'=>$tid);
        $count = $model->where($where)->count();
        $page = empty(I("post.p"))?1:I("post.p");
        $Page  = new AjaxPage($count,10);
        $res = $model->where(array('tid'=>$tid))->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('tid',$tid);
        $this->assign('tasters',$res);
        return $this->fetch();
    } 
    
    /**
     *  添加更新酒厂体验报名人员
     */
    public function addeditTaster(){ 
    	    	
    	$model = M('cellar_taste_form');
    	
    	//必须传入活动ID
        if(I('tid')&&is_numeric(I('tid')) || I('post.tid')){
        	$tid = I('tid');
        	$this->assign('tid',$tid);
        }
        else{
        	$return_arr = array(
                'status' => -1,
                'msg' => '操作失败',
                'data' => '未传入活动ID',
            );
            return  $this->ajaxReturn($return_arr);
        }
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('taster',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.name'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '姓名不能为空',
    		        'data' => '姓名不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}elseif (empty(I('post.phone'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '电话不能为空',
    		        'data' => '电话不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		$data['tid'] = $tid;
    		if($id){
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/tasterManage',array('tid'=>$tid))),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => $model->getLastsql(),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    
    /**
     *  删除报名体验人员
     */
    public function delTaster(){ 
    	    	
    	$model = M('cellar_taste_form');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/tasterManage',array('tid'=>$tid))),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/tasterManage',array('tid'=>$tid))),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    }
    
    
    
    /**
     *  窖藏直播预告
     */
    public function liveShow(){                
        $model = M('cellar_live_show');
        $page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->limit(($page-1)*$this->limit,$this->limit)->order("id desc")->select();
        $this->assign('pager',$Page->show());
        $this->assign('liveshows',$res);
        return $this->fetch();
    }
    
    
    /**
     *  增加修改窖藏直播预告
     */
    public function addeditShow(){                

        $model = M('cellar_live_show');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('liveshow',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '直播预告名称不能为空',
    		        'data' => '直播预告名称不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}elseif (empty(I('post.videourl'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '直播视屏源不能为空',
    		        'data' => '直播视屏源不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		elseif (empty(I('post.winery'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '酒厂名称不能为空',
    		        'data' => '酒厂名称不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		$data['livetime'] = strtotime($data['livetime']);
    		if($id){
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    			$data['addtime'] = time();
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/liveShow')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }
    
    
    /**
     *  删除窖藏直播预告
     */
    public function delShow(){ 
    	    	
    	$model = M('cellar_live_show');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/liveShow')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                    'status' => -1,
                    'msg' => '参数错误，请不要非法操作！',
                    'data' => array('url' => U('admin/Cellar/liveShow')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    }
    
    
    /**
     *  净含量规格
     */
    public function cellarQuantity(){                
        $model = M('cellar_quantity');
        $limit = $this->limit;
        $page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->limit(($page-1)*$limit,$limit)->order("val")->select();
        $this->assign('pager',$Page->show($Page));
        $this->assign('quantityList',$res);
        return $this->fetch();
    }
    
    /**
     *  添加更新窖藏净含量规格参数
     */
    public function addeditQuantity(){ 
    	    	
    	$model = M('cellar_quantity');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	$res = $model->where(array('id'=>$id))->find();
        	$this->assign('cellarquantity',$res);
        }
    	if(IS_POST){
    		$data = I('post.');
    		if(empty(I('post.val'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '净含量规格不能为空',
    		        'data' => '净含量规格不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		if($id){
    			$msg = $model->where(array('id'=>$id))->save($data);
    		}else{
    		    $msg = $model->add($data);
    		}
    	    if($msg){
    	    	$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/cellarQuantity')),
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	    else{
    	    	$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '数据入库失败，请联系系统管理员',
                );
    	    	return  $this->ajaxReturn($return_arr);
    	    }
    	}else{
    		return $this->fetch();
    	}             
    }    
    
    
    
    /**
     *  删除净含量
     */
    public function delQuantity(){ 
    	    	
    	$model = M('cellar_quantity');
        
        //若获取到ID则表示编辑，否则视为新增
        if(I('id')&&is_numeric(I('id'))){
        	$id = I('id');
        	if($model->where(array('id'=>$id))->delete()){
        		$return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('admin/Cellar/cellarQuantity')),
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}else{
        		$return_arr = array(
                    'status' => -1,
                    'msg' => '操作失败',
                    'data' => '删除数据失败，请联系管理员！',
                );
    	    	return  $this->ajaxReturn($return_arr);
        	}
        }
        else{
        	$return_arr = array(
                'status' => -1,
                'msg' => '参数错误，请不要非法操作！',
                'data' => array('url' => U('admin/Cellar/cellarQuantity')),
            );
	    	return  $this->ajaxReturn($return_arr);
        }
    	             
    }
    
    
    /**
     *  提酒管理
     */
    public function extractManage(){                
        $model = M('cellar_record');
        $page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where('types',0)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->where('types',0)->limit(($page-1)*$this->limit,$this->limit)->order('id desc')->select();
        foreach($res as $key=>$val){
        	$res[$key]['order_info'] = json_decode($val['order_info'],true);
        	$goods = M('goods')->where('goods_id',$val['goods_id'])->find();
        	$res[$key]['total'] = $goods['shop_price']*$val['num'];
        	$address = M('user_address')->where('address_id',$val['address_id'])->find();
        	$res[$key]['consignee'] = $address['consignee'];
        }
        //print_r($res);
        //exit;
        $this->assign('pager',$Page->show());
        $this->assign('data',$res);
        return $this->fetch();
    }
    
    /**
     *  提酒订单详细信息
     */
    public function addeditDetail(){
    	$id = I('get.id');
    	$model = M('cellar_record');
        $res = $model->where('id',$id)->find();
        //print_r($res);
        $res['order_info'] = json_decode($res['order_info'],true);

        $res['order_action'] = $action_log = json_decode($res['order_action'],true);
        $goods = M('goods')->where('goods_id',$res['goods_id'])->find();
    	$res['total'] = $goods['shop_price']*$res['num'];
    	$user = M('users')->where('user_id',$res['from_user_id'])->find();
    	$res['nickname'] = $user['nickname'];
    	$address = M('user_address')->where('address_id',$res['address_id'])->find();
    	$res['address'] = $address;
    	$orderGoods = M('order_goods')->where('rec_id',$res['rec_id'])->select();
    	
        if($res['order_info']['order_status'] == 0 && $res['order_info']['pay_status'] == 0){
        	$button['pay'] = '设为已付款'; 
        }
        if($res['order_info']['order_status'] == 0 && $res['order_info']['pay_status'] == 1){
            $button['pay_cancel'] = '退款'; 
            if($res['order_info']['extract_type']==1){
            	$button['stocking'] = '备货';
            }else{
            	$button['delivery'] = '去发货';
            }
            
        }	
        if($res['order_info']['order_status'] == 1 && $res['order_info']['pay_status'] == 1){
            $button['delivery_cancel'] = '取消发货'; 
        }
        if($res['order_info']['order_status'] == 5 && $res['order_info']['pay_status'] == 1){
            $button['remove'] = '移除'; 
        }
        $button['invalid'] = '无效';
        
        
        foreach($action_log as $key=>$val){
        	$action_user = M('users')->where('user_id',$val['action_user'])->find();
        	$action_log[$key]['action_user_name'] = $action_user['nickname'];
        }
        
        $this->assign('orderGoods',$orderGoods);
        $this->assign('order',$res);
        $this->assign('button',$button);
        $this->assign('action_log',$action_log);
    	return $this->fetch();
    }
    
    /**
     *  提酒订单操作日志
     */
    public function orderAction(){
    	
    	$recod_id = I('recod_id');
    	$action = I('action');
    	$action_note = I('note');
    	if(empty($recod_id) || empty($action)){
    		$return_arr = array(
                'status' => -1,
                'msg' => '非法参数',  
                'data' => 'recod_id或者action错误',
            );
	    	return  $this->ajaxReturn($return_arr);
    	}
    	$msg = cellar_order_action($action,$recod_id,$action_note);
    	if($msg){
    		$return_arr = array(
                'status' => 1,
                'msg' => '操作成功！',
                'data' => '',
            );
	    	return  $this->ajaxReturn($return_arr);
    	}else{
    		$return_arr = array(
                'status' => -1,
                'msg' => '操作失败！',
                'data' => '',
            );
	    	return  $this->ajaxReturn($return_arr);
    	}
    }
    
    /**
     *  提酒发货页面
     */
    public function delivery_info(){
    	
    	if(IS_POST){
    		
    		$recod_id = I('post.recod_id');
    		$action_note = I('post.note');
    		$invoice_no = I('post.invoice_no');
    		if($invoice_no){
    			$recod = M('cellar_record')->where('recod_id',$recod_id)->find();
    			$order_info = json_decode($recod['order_info'],true);
    			$order_info['invoice_no'] = $invoice_no;//存入快递单号
    			$r = M('cellar_record')->where('recod_id,$recod_id')->save(array('order_info'=>json_encode($order_info)));
    		}
    		else{
    			$this->success('操作失败',U('Admin/Cellar/delivery_info',array('recod_id'=>$recod_id)));
    		}
    		$action = 'delivery';
    		$msg = cellar_order_action($action,$recod_id,$action_note);
    	    if($r&&$msg){
				$this->success('操作成功',U('Admin/Cellar/addeditDetail',array('id'=>$recod_id)));
			}else{
				$this->success('操作失败',U('Admin/Cellar/delivery_info',array('recod_id'=>$recod_id)));
			}
    	}else{
    		$id = I('get.recod_id');
	    	$model = M('cellar_record');
	        $res = $model->where('id',$id)->find();
	     
	        $res['order_info'] = json_decode($res['order_info'],true);
	    	
	    	$address = M('user_address')->where('address_id',$res['address_id'])->find();
	    	$orderGoods = M('order_goods')->where('rec_id',$res['rec_id'])->select();
	    		       
	        $this->assign('orderGoods',$orderGoods);
	        $this->assign('recod',$res);
	        $this->assign('order',$res['order_info']);
	        $this->assign('address',$address);
	        return $this->fetch();
    	}
    }
    
    /**
     *  赠酒管理
     */
    public function giveManage(){                
        $model = M('cellar_record');
        $page = empty(I("post.p"))?1:I("post.p");
        $count = $model->where('types',1)->count();
        $Page  = new AjaxPage($count,10);
        $res = $model->where('types',1)->limit(($page-1)*$this->limit,$this->limit)->order('id desc')->select();
        foreach($res as $key=>$val){
        	$goods = M('goods')->where('goods_id',$val['goods_id'])->find();
        	$res[$key]['total'] = $goods['shop_price']*$val['num'];
        	$from_user = M('users')->where('user_id',$val['from_user_id'])->find();
        	$res[$key]['from_user'] = $from_user['nickname'];
        	$to_user = M('users')->where('user_id',$val['to_user_id'])->find();
        	$res[$key]['to_user'] = $to_user['nickname'];
        }
        $this->assign('pager',$Page->show());
        $this->assign('data',$res);
        return $this->fetch();
    }
    
    /**
     *  赠酒记录详细信息
     */
    public function giveDetail(){   
        $id = I('id');
        $model = M('cellar_record');
        $res = $model->where('id',$id)->find();
        $from_user = M('users')->where('user_id',$res['from_user_id'])->find();
    	$res['from_user'] = $from_user;
    	$to_user = M('users')->where('user_id',$res['to_user_id'])->find();
    	$res['to_user'] = $to_user;
        $orderGoods = M('order_goods')->where('rec_id',$res['rec_id'])->select();
        $this->assign('giveInfos',$res);
        $this->assign('orderGoods',$orderGoods);
        return $this->fetch();
    }
    
    
    
    
}